import entity.Emp;
import mapper.EmpMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

public class EmpMapperTest {

    private EmpMapper empMapper;

    @Test
    public void test01() throws IOException {

        SqlSession sqlSession = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml")).openSession(true);

        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);


//        Emp emp = empMapper.selectOneById(3);
//        System.out.println(emp);
//
//        emp.setEmpName("李四");
//
//
//        int i = empMapper.updateEmp(emp);
//
//        System.out.println(i);


//        Emp emp2 = empMapper.selectOneById(3);
//        System.out.println(emp2);


        //添加  我希望   添加成功后 获得   新增员工的id值

        Emp newEmp = new Emp();
        newEmp.setEmpName("找哦六");
        newEmp.setBirthday(LocalDateTime.now());
        newEmp.setSalary(new BigDecimal(8000));
        newEmp.setDepartmentId(2);

        int j = empMapper.insertEmp(newEmp);

        System.out.println(j);

        System.out.println("新员工的id:" + newEmp.getEmpId());


        List<Emp> emps = empMapper.selectAll();

        emps.forEach(System.out::println);


        //

    }


    @Test
    public <SqlSession> void test03() throws IOException {
        SqlSession sqlSession = (SqlSession) new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml")).openSession(true);

//        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
//        //多参数查询  姓名 薪资

//        Emp emp = empMapper
//                .selectOneByNameAndSalary("张三",
//                        BigDecimal.valueOf(4000));

        //对象参数查询
//        Emp selectEmp = new Emp();
//        selectEmp.setEmpName("张三");
//        selectEmp.setSalary(BigDecimal.valueOf(4000));

//        Emp emp2 = empMapper
//                .selectOneByNameAndSalary(selectEmp);

//        System.out.println(emp2);


        //模糊查询

//        List<Emp> emps = empMapper.selectAllEmpByLikeName("三");
//
//        emps.forEach(System.out::println);

        //两表连接查询

//        List list = empMapper.selectAllEmpAndDepartName();

//        list.forEach(System.out::println);


        //resultMap的使用

        //返回 map集合


        List<Map> maps = empMapper.selectAllEmpAndDepartNameByMap();

        System.out.println(maps);
    }

    private EmpMapper getMapper(Class<EmpMapper> empMapperClass) {
        return null;
    }
}